// Copyright (c) MatrixFramework. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.Threading;
using System.Threading.Tasks;
using MatrixFramework.Keycloak.Abstractions.Models;
using MatrixFramework.Core.Results;

namespace TenantManagementService.Migration.Services;

/// <summary>
/// 租户管理服务接口（新版 - 使用MatrixFramework.Keycloak）
/// </summary>
public interface ITenantManagementService
{
    /// <summary>
    /// 创建租户
    /// </summary>
    /// <param name="tenantInfo">租户信息</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns>创建结果</returns>
    Task<Result<TenantCreationResult>> CreateTenantAsync(TenantInfo tenantInfo, CancellationToken cancellationToken = default);

    /// <summary>
    /// 删除租户
    /// </summary>
    /// <param name="tenantId">租户ID</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns>删除结果</returns>
    Task<Result> DeleteTenantAsync(string tenantId, CancellationToken cancellationToken = default);

    /// <summary>
    /// 同步租户到Keycloak
    /// </summary>
    /// <param name="tenantId">租户ID</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns>同步结果</returns>
    Task<Result<TenantSyncResult>> SyncTenantAsync(string tenantId, CancellationToken cancellationToken = default);

    /// <summary>
    /// 批量同步租户
    /// </summary>
    /// <param name="tenantIds">租户ID列表</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns>批量同步结果</returns>
    Task<Result<BatchTenantSyncResult>> SyncTenantsAsync(List<string> tenantIds, CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取租户同步状态
    /// </summary>
    /// <param name="tenantId">租户ID</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns>同步状态</returns>
    Task<Result<TenantSyncStatus>> GetTenantSyncStatusAsync(string tenantId, CancellationToken cancellationToken = default);

    /// <summary>
    /// 测试租户连接
    /// </summary>
    /// <param name="tenantId">租户ID</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns>连接测试结果</returns>
    Task<Result<bool>> TestTenantConnectionAsync(string tenantId, CancellationToken cancellationToken = default);
}

/// <summary>
/// 租户创建结果
/// </summary>
public class TenantCreationResult
{
    /// <summary>
    /// 租户ID
    /// </summary>
    public string TenantId { get; set; } = string.Empty;

    /// <summary>
    /// 租户名称
    /// </summary>
    public string TenantName { get; set; } = string.Empty;

    /// <summary>
    /// 创建的Realm名称
    /// </summary>
    public string? RealmName { get; set; }

    /// <summary>
    /// 管理员用户名
    /// </summary>
    public string? AdminUsername { get; set; }

    /// <summary>
    /// 管理员密码（仅在创建时返回）
    /// </summary>
    public string? AdminPassword { get; set; }

    /// <summary>
    /// 管理控制台URL
    /// </summary>
    public string? AdminConsoleUrl { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime CreatedAt { get; set; }

    /// <summary>
    /// 是否成功
    /// </summary>
    public bool Success { get; set; }

    /// <summary>
    /// 消息
    /// </summary>
    public string Message { get; set; } = string.Empty;
}